import axios from "axios";

const listModule = {
  namespaced:true,
  state: {
    list:[],
    storeDetails:{},
    images:[]
  },
  mutations: {
    INIT_DATA(state,items){
      state.list  = [...items];
    },
    GET_STORE_DETAILS(state,details){
      state.storeDetails = {...details};

      state.images = state.storeDetails.imgs.split(",");
    },
    SORT_LIST(state,rules){
      let sortFunc = null;
      switch (rules){
        case "价格低到高":
          sortFunc = function(a,b){
            return a.actualPrice-b.actualPrice;
          }
          break;
        case "价格高到低":
          sortFunc = function(a,b){
            return b.actualPrice-a.actualPrice;
          }
          break;
        default:
          sortFunc = function(a,b){
            return a.actualPrice-b.actualPrice;
          }
          break;
      }
      state.list.sort(sortFunc)
    }
  },
  actions: {
    INIT_DATA(context){
      axios.get("http://api.kudesoft.cn/tdk/goods").then((res)=>{
        console.log(res.data.data.data.list);
        context.commit("INIT_DATA",res.data.data.data.list)
      }).catch((err)=>{
        console.log(err)
      })
    },
    GET_STORE_DETAILS(context,id){
      axios.get("http://api.kudesoft.cn/tdk/details",{
        params:{
          id:id
        }
      }).then((res)=>{
        console.log(res.data.data.data)
        context.commit("GET_STORE_DETAILS",res.data.data.data)
      }).catch((err)=>{
        console.log(err)
      })
    }
  },
  modules: {
    
  }

}

export default listModule;